package iso.isoLogic
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	import com.greensock.loading.XMLLoader;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.system.LoaderContext;
	import flash.utils.setTimeout;
	import iscript.gameFramework.IsoEngine.core.IsoScene;
	import iscript.gameFramework.IsoEngine.data.IsoPosition;
	import iscript.gameFramework.IsoEngine.display.IsoBox;
	import iscript.gameFramework.IsoEngine.display.IsoGrid;
	import iscript.gameFramework.IsoEngine.display.IsoTileGrid;
	import iscript.gameFramework.IsoEngine.util.IsoUtil;
	import iscript.uiFramework.components.toolButton.*;
	
 
	public class isomain extends Sprite
	{
		private var _uint:Number = 45;
		
	
		[Embed(source="../../../bin/texture/1.png")]
		public var tb:Class;
		[Embed(source="../../../bin/texture/2.png")]
		public var front:Class;
		[Embed(source="../../../bin/texture/3.png")]
		public var right:Class
		
		[Embed(source="../../../bin/texture/BHZ.gif")]
		public var bhz:Class;
		
		[Embed(source="../../../bin/texture/R.gif")]
		public var rw:Class;
		
		public function get usefulUnit():Number
		{
			return Math.round(_uint * 0.71);
		}
		public var scene:IsoScene
		public function isomain()
		{
			
			trace(Math.round(32 * 1.42))
			
			trace(Math.round(1.1), Math.round(1.41), Math.round(1.4), Math.round(1.51), Math.round(1.5))
			
			trace(16 >> 1, 16 << 1, "位移")
			
			scene = new IsoScene();
			scene.x = 500;
			scene.y = 100;
			
			var isoGrid:IsoGrid = new IsoGrid()
			isoGrid.setGridSize(10, 10, 0)
			scene.setIsoGrid(isoGrid);
			scene.render(this);
			
			//scene.addChild(isoGrid)
			
			 var isotile:IsoTileGrid = new IsoTileGrid()
			     isotile.moveTo(3,0,3)
			     //isotile.position = new Vector3D(4 * 32, 0, 0);
			    isotile.opaqueBackground = 0x808000;
			//scene.addChild(isotile) 
			
			setTimeout(function():void
				{
				   isotile.moveAnimationTo(2, 0, 0);   
				   //isotile.moveTo(2, 0, 0);
				}, 1000);
				
				
						var box:IsoBox = new IsoBox(35,35,70)
						    //box.alpha = 0.5
							box.position =new Vector3D(32, 0, 32);
							//box.x =100
						 	box.topBitmap = Bitmap(new tb()).bitmapData;
							box.frontBitmap = Bitmap(new front()).bitmapData;
							box.rightBitmap = Bitmap(new right()).bitmapData; 
							
							var b:DisplayObject = new rw();
							    //b.scaleX = 0.7
								//b.scaleY = 0.7
								b.x -= b.width / 2
								b.y -= b.height-(35>>1);
								
							box.container.addChild(b)
							scene.addIsoChild(box);
							
							
							//box.IsoX = 10
							//box.IsoX = 32
							//box.offX = 32
				 			//box.offY = 32>>1
						  	//box.IsoX =  (-32>>1)  +32;
							
						 	trace(box.position,IsoUtil.isoToScreen(box.position))
						//	trace(box.getIsoPosition())
							box.setIsoPosition(new IsoPosition(2, 0, 2));
							trace(box.position,box.getIsoPosition(),IsoUtil.isoToScreen(box.position))
							
							
					/*	var box2:IsoBox = new IsoBox(50,35,10)
						   // box2.alpha = 0.5
						   // box2.IsoX = 32>>1
						    //box2.IsoZ = 32>>1
							//box.position = new Vector3D(0, 0, 0);
							//box.x =100
							box2.position = new Vector3D(32, -60, 32);
						//	box2.height = 100
						//    box2.IsoY = -10
						    box2.topBitmap = Bitmap(new tb()).bitmapData;
							box2.frontBitmap = Bitmap(new front()).bitmapData;
							box2.rightBitmap = Bitmap(new right()).bitmapData;  
							scene.addIsoChild(box2)	*/
							
							//box.moveAnimationTo(5,0,5);
							
				
/*			var c:int = 3
			var ch:int=0
				for (var i:int = 0; i < 3; i++) 
				{
						var box:IsoBox = new IsoBox(32,32,32*i) 
							box.x = 300;
							box.y = 300;
							box.addEventListener(MouseEvent.MOUSE_OVER,boxOverLogic)
								box.addEventListener(MouseEvent.MOUSE_OUT, boxOutLogic)
				 
							
							box.position =  new Vector3D(5* 32-(32 >> 1), -32*i, 5*32 - (32 >> 1))
							//box.position =  new Vector3D(c * 32 - (32 >> 1), ch * -32, 0 - (32 >> 1))
							box.topBitmap = Bitmap(new tb()).bitmapData;
							box.frontBitmap = Bitmap(new front()).bitmapData;
							box.rightBitmap = Bitmap(new right()).bitmapData;
							//box.position =  new Vector3D(c*32-(0),(ch * -32),0 - (0))
							scene.addIsoChild(box);
					 
				}*/
		
		addEventListener(Event.ADDED_TO_STAGE,addStageLogic)
		
			//var  pos:Vector3D = new Vector3D();
		/*	return;
		
		   trace(IsoUtil.isoToScreen(pos).x, IsoUtil.isoToScreen(pos).y)
		
		   var ball:Ball = new Ball(5)
		   ball.x = IsoUtil.isoToScreen(pos).x
		   ball.y = IsoUtil.isoToScreen(pos).y
		   addChild(ball);
		
		
		   var d:Diamond = new Diamond(45, 45);
		   d.x = ball.x;
		   d.y = ball.y;
		   //d.opaqueBackground = 0xC0C0C0
		   addChild(d);
		
		   pos = new Vector3D(usefulUnit,0,0);
		   trace(IsoUtil.isoToScreen(pos).x,IsoUtil.isoToScreen(pos).y)
		
		   ball = new Ball(5)
		   ball.x = IsoUtil.isoToScreen(pos).x
		   ball.y = IsoUtil.isoToScreen(pos).y
		   addChild(ball);
		
		   d =  new Diamond(45*1, 45*1)
		   d.x = ball.x;
		   d.y = ball.y;
		   //d.opaqueBackground = 0xFF00FF
		 addChild(d);*/
		
			// pos = new Vector3D(32,32,0);
		
			//trace(IsoUtil.isoToScreen(pos).x,IsoUtil.isoToScreen(pos).y);
		}
		
		private function boxOutLogic(e:MouseEvent):void 
		{
			 IsoBox(e.currentTarget).alpha = 1
		}
		
		private function boxOverLogic(e:MouseEvent):void 
		{
		    IsoBox(e.currentTarget).alpha = 0.5
		}
		
		private function addStageLogic(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addStageLogic);
						
		scene.addEventListener(MouseEvent.CLICK, clickLogic)
		}
		
		private function clickLogic(e:MouseEvent):void
		{
			var posMouse:Point = new Point(scene.mouseX,scene.mouseY)
			var pos:Vector3D = IsoUtil.screenToIso(posMouse);
			trace(posMouse.x, posMouse.y, "<<<<<", pos.x, pos.y, pos.z, ">>>>>>>>>>>>")
			
			//	pos.y = _yy++
			//	trace(pos.x / usefulUnit, Math.round(pos.x / usefulUnit));
			
			pos.x = Math.round(pos.x / usefulUnit) * usefulUnit;
			pos.y = Math.round(pos.y / usefulUnit) * usefulUnit;
			pos.z = Math.round(pos.z / usefulUnit) * usefulUnit;
			
			trace("Vector3D:", pos.x, pos.y, pos.z, "Tile:", IsoUtil.isoToScreen(pos).x, IsoUtil.isoToScreen(pos).y);
		}
	}

}